#include "Model/Monkey.h"
#include "cocos2d.h"
#include "cocos-ext.h"
#include "Config.h"
#include "HelloWorldScene.h"

using namespace cocos2d;
using namespace cocos2d::extension;

Monkey * Monkey::create()
{
	Monkey *nMonkey = new Monkey();
    if (nMonkey && nMonkey->init())
    {
        nMonkey->autorelease();
        return nMonkey;
    }
    else
    {
        CC_SAFE_DELETE(nMonkey);
        return NULL;
    }
}

bool Monkey::init()
{
	CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo(kImageMonkey, kPlistMonkey, kJsonMonkey);
	mArmature = CCArmature::create(kNameMonkey);
	mArmature->setPosition(0,0);

	mScale = 1.0f;

	mArmature->setScaleX(mScale * isTurningLeft ); // Set huong quay mat cua con khi
	mArmature->setScaleY(mScale);

    this->addChild(mArmature); 

	fSetStatus(Idle); // Trang thai ban dau cua con khi
	mCurrentStatus = Idle;
    mNewStatus = Idle;

	mCurrentDirection = isTurningLeft ;
	mNewDirection = isTurningLeft;

	fSetHeath(MKHEATHFULL); // Luong mau ban dau cua con khi

	fEffect();
	mArmature->getAnimation()->setMovementEventCallFunc(this, movementEvent_selector(HelloWorld::animationEvent));

	// Pre load music
	fPreLoadMusic();

	this->schedule(schedule_selector(Monkey::update));
	this->schedule(schedule_selector(Monkey::fDecreaseHeath));
	return true;
}

void Monkey::fPreLoadMusic()
{
	CCLog("PreLoad music !!!!");
	//CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(kSoundBroken);
}

void Monkey::update(float dt)
{

	// Kiem tra huong xoay hien tai cua con khi
	if(mCurrentDirection != mNewDirection) //huong khong thay doi
	{
		mCurrentDirection = mNewDirection;
		fChangeDirection();
	}
		
	if (mCurrentStatus != mNewStatus) // trang thai khong thay doi
    {
        mCurrentStatus = mNewStatus;
        fEffect();
    }

}

void Monkey::fChangeDirection()
{
	
	mArmature->setScaleX(mScale * fGetDirection());
	mArmature->setScaleY(mScale);
}

void Monkey::fRunChangeDirection(float widthtree)
{
	CCSize nSize = CCDirector::sharedDirector()->getWinSize();

	CCRect nRectBodyMonkey = mArmature->getBone("Layer6")->getDisplayRenderNode()->boundingBox();

	if(mNewDirection > 0)
	{
		setPositionX(nSize.width/2 + nRectBodyMonkey.size.width/2 + widthtree);
	}
	else
	{
		setPositionX(nSize.width/2 - nRectBodyMonkey.size.width/2 - widthtree);
	}
}

void Monkey::fEffect()
{
	switch(mCurrentStatus)
	{
			//action
			//play sound effect here

		case Idle: //dung yen
			mArmature->getAnimation()->play("treo"); 
			// CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(kSoundBroken);
			break;

		case isJump: //treo
			mArmature->getAnimation()->play("nhaybamcay"); 
			// CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(kSoundBroken);
			break;

		case isDie: //roi
			mArmature->getAnimation()->play("rot"); 
			// CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(kSoundBroken);
			break;

		case isJumpLeft: //chet
			mArmature->getAnimation()->play("treo");
			// CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(kSoundBroken);
			break;

		case isJumpRight: //bay qua cay
			mArmature->getAnimation()->play("treo"); 
			// CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(kSoundBroken);
			break;

		default: 
			CCLog("Default effect broken !!");
			break;
	}
}

void Monkey::fDecreaseHeath(float delta)
{
	if(mHeath == MKHEATHZERO) return;
	--mHeath;

	//CCLog("Update UI to Parent");
	((HelloWorld*)getParent())->fUpdateHeath(mHeath);

}

void Monkey::fIncreaseHeath(int type)
{
	switch(type)
	{
	case FBANANA:
		mHeath += kHeathInBanana;
		break;
	case FAPPLE:
		mHeath += kHeathInApple;
		break;
	case FPEAR:
		mHeath += kHeathInPear;
		break;
	default:
		break;
	}
	mHeath > MKHEATHFULL ? mHeath = MKHEATHFULL : mHeath += MKHEATHZERO;
}

void Monkey::draw()
{
	/*CCRect c = this->fGetArmature()->boundingBox();
	ccDrawRect(ccp(c.getMinX(),c.getMaxY()),ccp(c.getMaxX(),c.getMinY()));*/
}